﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if !WINDOWS_PHONE


#endif

namespace DARE
{
    public class CIsoCamera : CCamera
    {

    #region fields

        private Vector3 m_followPosition;
        private CNode m_followNode;
        private Vector3 m_followLookAtOffset;
        private float m_zoom = 1;
        private float m_zoomMin = 0;
        private float m_zoomMax = 1;

    #endregion

    #region properties

        /// <summary>
        /// Smooth the camera's position.
        /// Get if the camera's position is smoothed.
        /// Set the smooth on the camera.
        /// </summary>
        public bool Smooth
        {
            get { return m_node.SmoothPosition; }
            set { m_node.SmoothPosition = value; }
        }

        /// <summary>
        /// Amount of position's smooth. See node SmoothPosition for more informations.
        /// Get the amount of smooth on the camera.
        /// Set the amount of smooth on the camera.
        /// </summary>
        public float SmoothAmount
        {
            get { return m_node.SmoothPositionAmount; }
            set { m_node.SmoothPositionAmount = value; }
        }

        /// <summary>
        /// The follow position is relative to the node's position it follows.
        /// A camera with a follow position of 0, 1, 0 will always be over the node it follows.
        /// Get the follow position.
        /// Set the follow position.
        /// </summary>
        public Vector3 FollowPosition
        {
            get { return m_followPosition; }
            set { m_followPosition = value; }
        }

        /// <summary>
        /// The follow node is the node that the camera will automaticaly follow at a "FollowPosition" offset.
        /// Get the node if follows.
        /// Set the node to follow.
        /// </summary>
        public CNode FollowNode
        {
            get { return m_followNode; }
            set { m_followNode = value; }
        }

        /// <summary>
        /// By default the camera look at the node it follows. But it 
        /// can be changed with an offset.
        /// Get the lookat offset.
        /// Set the lookat offset.
        /// </summary>
        public Vector3 FollowLookAtOffset
        {
            get { return m_followLookAtOffset; }
            set { m_followLookAtOffset = value; }
        }

        /// <summary>
        /// Camera's zoom to the node it follows.
        /// A zoom of 0 is at the position of the node it follows while a zoom of 1 
        /// is at the FollowPosition.
        /// Get the zoom.
        /// Set the zoom according to the ZoomMin and ZoomMax values.
        /// </summary>
        public float Zoom
        {
            get { return m_zoom; }
            set
            {
                if ((value >= m_zoomMin) &&
                    (value <= m_zoomMax))
                    m_zoom = value;
                else if (value > m_zoomMax)
                    m_zoom = m_zoomMax;
                else if (value < m_zoomMin)
                    m_zoom = m_zoomMin;
            }
        }

        /// <summary>
        /// Minimum zoom of the camera.
        /// See Zoom for more informations on camera's zoom.
        /// Get the minimum zoom.
        /// Set the minimum zoom.
        /// </summary>
        public float ZoomMin
        {
            get { return m_zoomMin; }
            set 
            { 
                if (m_zoomMin <= m_zoomMax)
                    m_zoomMin = value;
            }
        }

        /// <summary>
        /// Maximum zoom of the camera.
        /// See Zoom for more informations on camera's zoom.
        /// Get the maximum zoom.
        /// Set the maximum zoom.
        /// </summary>
        public float ZoomMax
        {
            get { return m_zoomMax; }
            set
            {
                if (m_zoomMax >= m_zoomMin)
                    m_zoomMax = value;
            }
        }

    #endregion

    #region ctor

        /// <summary>
        /// Create an iso camera with a name.
        /// Iso camera is no affected by the orientation of the node it follows.
        /// </summary>
        /// <param name="name">Name of the camera.</param>
        public CIsoCamera(string name) :
            base(name)
        { }

    #endregion

    #region methods

        /// <summary>
        /// Update the camera, called by the camera manager.
        /// </summary>
        /// <param name="gameTime">GameTime.</param>
        public override void Update(GameTime gameTime)
        {
            if (!AttachedToNode)
            {
                m_node.SetPosition(m_followPosition * m_zoom + m_followNode.Position, CNode.ESpace.WORLD);
                m_node.LookAt(m_followLookAtOffset + m_followNode.Position);
            }
            m_needUpdateView = true;
        }

    #endregion

    }
}
